﻿using Microsoft.AspNet.SignalR;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FangDuShi_Admin.Controllers
{
    public class ApiController : Controller
    {
        //
        // GET: /Api/
        public string Index()
        {
            return "1212";
        }

        public string FileUpload()
        {
            Stream st = Request.InputStream;
            long ll = st.Length;
           // object obj = Request.Form["profile_picture"];
            for (int i = 0; i < Request.Files.Count; i++)
            {
                HttpPostedFileBase upload=Request.Files[i];
                string aa = Request.Files.GetKey(i);
                //if (upload.ContentLength>0 )
                upload.SaveAs(Server.MapPath("/Content/"+Guid.NewGuid()+".jpg"));
                foreach (var key in ChatHub.ChatConnection.Keys)
                {
                   
                    foreach (string clientId in  ChatHub.ChatConnection.GetConnections(key).ToList())
                    {
                        GlobalHost.ConnectionManager.GetHubContext<ChatHub>().Clients.Client(clientId).addNewMessageToPage("", "{'type':'voice','message':'55.amr'}}");

                    }
                    
                }
            }
            string message = "{'type':'msg','message':'file success'}";
            return message;


            
           
           
            //GlobalHost.ConnectionManager.GetHubContext<ChatHub>().Clients.

                
        }
        [ValidateInput(false)]
        public string FileUpload2()
        {
            this.ValidateRequest = false;
            Stream st = Request.InputStream;
            long ll = st.Length;
            object obj = Request.Form["profile_picture"];

            object picUrl = Request.Form["picUrl"];
            object title = Request.Form["title"];
            for (int i = 0; i < Request.Files.Count; i++)
            {
                HttpPostedFileBase upload = Request.Files[i];
                string aa = Request.Files.GetKey(i);
                //if (upload.ContentLength>0 )
                upload.SaveAs(Server.MapPath("/Content/"+Guid.NewGuid()+".jpg"));
                foreach (var key in ChatHub.ChatConnection.Keys)
                {

                    foreach (string clientId in ChatHub.ChatConnection.GetConnections(key).ToList())
                    {
                        GlobalHost.ConnectionManager.GetHubContext<ChatHub>().Clients.Client(clientId).addNewMessageToPage("", "{'type':'voice','message':'55.amr'}}");

                    }

                }
            }
            string message = "{'type':'msg','message':'file success'}";
            return message;





            //GlobalHost.ConnectionManager.GetHubContext<ChatHub>().Clients.


        }
	}
}